﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using bdnt_pets_system.Tools;
using System.Windows.Data;
using bdnt_pets_system.DataServiceReference;
using bdnt_pets_system_Entity;

namespace bdnt_pets_system.PageViews.DeptManager
{
    public partial class NewDeptControl : UserControl
    {

        DataServiceReference.DataServiceClient dtClient;
        public NewDeptControl()
        {
            InitializeComponent();
            dtClient = new DataServiceReference.DataServiceClient();
            dept = new Tbl_Department();
            InitEvnet();
            BingData();
        }

        void BingData()
        {
            tbNo.SetBinding(TextBox.TextProperty, new Binding("DeptNo")
            {
                Mode = BindingMode.TwoWay,
                NotifyOnValidationError = true,
                ValidatesOnExceptions = true
            });
            tbName.SetBinding(TextBox.TextProperty, new Binding("DeptName")
            {
                Mode = BindingMode.TwoWay,
                NotifyOnValidationError = true,
                ValidatesOnExceptions = true
            });
            tbType.SetBinding(TextBox.TextProperty, new Binding("DeptType")
            {
                Mode = BindingMode.TwoWay,
                NotifyOnValidationError = true,
                ValidatesOnExceptions = true
            });
            tbMNo.SetBinding(TextBox.TextProperty, new Binding("ManagerNo")
            {
                Mode = BindingMode.TwoWay,
                NotifyOnValidationError = true,
                ValidatesOnExceptions = true
            });
            tbR.SetBinding(TextBox.TextProperty, new Binding("Remark") { Mode = BindingMode.TwoWay });
        }

        public NewDeptControl(Tbl_Department td)
        {
            InitializeComponent();
            dtClient = new DataServiceReference.DataServiceClient();
            InitEvnet();
            BingData();
            dept = td;
        }

        public Tbl_Department dept
        {
            get { return this.DataContext as Tbl_Department; }
            set { this.DataContext = value; }
        }

        void InitEvnet()
        {
            btnCancel.Click += new RoutedEventHandler(btnCancel_Click);
            btnOk.Click += new RoutedEventHandler(btnOk_Click);

            dtClient.SaveDeptCompleted += new EventHandler<SaveDeptCompletedEventArgs>(dtClient_SaveDeptCompleted);
            dtClient.GetDepartmentCompleted += new EventHandler<GetDepartmentCompletedEventArgs>(dtClient_GetDepartmentCompleted);

        }

        void dtClient_SaveDeptCompleted(object sender, SaveDeptCompletedEventArgs e)
        {
            if (e.Result)
            {
                MessageBox.Show("保存成功！", "系统提示", MessageBoxButton.OK);
                BingData();
                App.ToPage(new DeptManagerControl());
            }
            else
            {
                MessageBox.Show("保存失败！", "系统提示", MessageBoxButton.OK);
                return;
            }
        }

        void dtClient_GetDepartmentCompleted(object sender, GetDepartmentCompletedEventArgs e)
        {
            dept = e.Result;
            BingData();
         }

        void btnOk_Click(object sender, RoutedEventArgs e)
        {
            tbNo.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            tbName.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            tbType.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            tbMNo.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            if (vs.Errors.Count() > 0)
            {
                errorMsg.Text = "数据填写不全！";
                return;
            }
            dtClient.SaveDeptAsync(dept);
        }

        void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            App.ToPage(new DeptManagerControl());
        }

    }
}
